<?php

namespace App\Http\Controllers\Api\Oauth\Auth;

use App\Http\Controllers\Api\Oauth\Auth\ApiBaseController as BaseController;
use Illuminate\Http\Request;
use App\Models\Oauth\UserModel;
use App\Models\Club\MemberModel;
use App\Services\Oauth\LoginService;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Cookie;
use App\Models\Oauth\CodeModel;
use App\Models\Oauth\AuthorizeModel;
use App\Services\Oauth\UserService;
use App\Services\Oauth\User;
use App\Services\Oauth\RegistService;
use App\Exceptions\Response\Service\ServiceCode;
use App\Exceptions\Response\Service\ServiceException;
use App\Services\Oauth\OauthorService;
use App\Services\Utils\TencentYun\ImageService;
use TencentYun\Image\ImageV2;

/**
 * 用户信息维护接口
 */
class ApiUserController extends BaseController
{

    /**
     * 获取用户基本信息
     */
    public function getUser(UserService $userService)
    {
        $user = $this->identity()->getUser();
        $result = $userService->getUserInfo($user['id']);
        return $this->response->setResponseData($result)->getResponse();
    }

    /**
     * 修改密码
     */
    public function changePassword(Request $request)
    {
        $this->validate($request, [
            'mobile' => 'required',
            'password' => 'required',
        ]);
        //手机号的国家码，中国：86
        $nationCode = $request->input('nationCode', 86);
        $mobile = $request->input('mobile', '');
        $passWord = $request->input('password', '');
        $registService = new RegistService('', $mobile, $passWord, '');
        //验证手机格式
        $registService->checkMobileFormat($nationCode);
        $userService = new UserService();
        //验证手机号码是否注册
        $id = $userService->checkMobile($mobile);
        if(!$id){
            throw new ServiceException(ServiceCode::SERVICE_OAUTH_MOBILE_IS_NOT_USERD, '', []);
        }
        //验证密码格式
        $registService->checkPasswordFormat();
        //修改密码
        $userService->changePassword($id, $passWord);
        $data['status'] = 1;
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     *设置头像
     */
    public function setAvatar(Request $request, UserService $userService)
    {
        $this->validate($request, [
            'avatar' => 'required',
        ]);
        $avatar = $request->input('avatar', '');
        $user = $this->identity()->getUser();
        $userInfo = $userService->getUserInfo($user['id']);
        if ($userInfo['avatarId'] != $avatar) {
            $data['avatar'] = $avatar;
            if ($userService->setUserInfo($user['id'], $data)) {
                $result['status'] = 1;
            }
        } else {
            $result['status'] = 1;
        }
        return $this->response->setResponseData($result)->getResponse();
    }

    /**
     *设置用户名
     */
    public function setUserName(Request $request, UserService $userService)
    {
        $this->validate($request, [
            'username' => 'required',
        ]);
        $userName = $request->input('username', '');
        $user = $this->identity()->getUser();
        $userInfo = $userService->getUserInfo($user['id']);
        if ($userInfo['username'] != $userName) {
            $data['username'] = $userName;
            if ($userService->setUserInfo($user['id'], $data)) {
                $userService->setBbsUserInfo($user['bbs_uid'], $data);
                $result['status'] = 1;
            }
        } else {
            $result['status'] = 1;
        }
        return $this->response->setResponseData($result)->getResponse();
    }

    /**
     *设置姓名
     */
    public function setName(Request $request, UserService $userService)
    {
        $this->validate($request, [
            'name' => 'required',
        ]);
        $name = $request->input('name', '');
        $user = $this->identity()->getUser();
        $userInfo = $userService->getUserInfo($user['id']);
        if ($userInfo['name'] != $name) {
            $data['name'] = $name;
            if ($userService->setUserInfo($user['id'], $data)) {
                $result['status'] = 1;
            }
        } else {
            $result['status'] = 1;
        }
        return $this->response->setResponseData($result)->getResponse();
    }

    /**
     *设置性别
     */
    public function setSex(Request $request, UserService $userService)
    {
        $this->validate($request, [
            'sex' => 'required',
        ]);
        $sex = $request->input('sex', '');
        $user = $this->identity()->getUser();
        $userInfo = $userService->getUserInfo($user['id']);
        if ($userInfo['sexid'] != $sex) {
            $data['sex'] = $sex;
            if ($userService->setUserInfo($user['id'], $data)) {
                $result['status'] = 1;
            }
        } else {
            $result['status'] = 1;
        }
        return $this->response->setResponseData($result)->getResponse();
    }

    /**
     *设置用户地理信息
     */
    public function setUserDistrict(Request $request, UserService $userService)
    {
        $province = $request->input('province', '');
        $city = $request->input('city', '');
        $area = $request->input('area', '');
        $town = $request->input('town', '');
        $address = $request->input('address', '');
        if ($province) {
            $data['province'] = $province;
        }
        if ($city) {
            $data['city'] = $city;
        }
        if ($area) {
            $data['area'] = $area;
        }
        if ($town) {
            $data['town'] = $town;
        }
        if ($address) {
            $data['address'] = $address;
        }
        $user = $this->identity()->getUser();
        if ($userService->updateUserProfile($user['id'], $data)) {
            $result['status'] = 1;
        }
        return $this->response->setResponseData($result)->getResponse();
    }

    /**
     *绑定新手机号
     */
    public function boundMobile(Request $request, UserService $userService)
    {
        $this->validate($request, [
            'mobile' => 'required',
            'code' => 'required'
        ]);
        //手机号的国家码，中国：86
        $nationCode = $request->input('nationCode', 86);
        $mobile = $request->input('mobile', '');
        //短信验证码
        $code = $request->input('code', '');
        $registService = new RegistService('', $mobile, '', $code);
        //验证手机格式
        $registService->checkMobileFormat($nationCode);
        //验证手机号码是否注册
        $id = $userService->checkMobile($mobile);
        if ($id) {
            throw new ServiceException(ServiceCode::SERVICE_OAUTH_MOBILE_IS_USERD, '', []);
        }
        //验证短信验证码
        $registService->checkSmsCode();
        //绑定手机号
        $user = $this->identity()->getUser();
        $userInfo = $userService->getUserInfo($user['id']);
        if ($userInfo['mobile'] == $mobile) {
            throw new ServiceException(ServiceCode::SERVICE_OAUTH_MOBILE_FORMAT_IS_WRONG, '', []);
        }
        $data['mobile'] = $mobile;
        if ($userService->setUserInfo($user['id'], $data)) {
            $result['status'] = 1;
        }
        return $this->response->setResponseData($result)->getResponse();
    }

    /**
     * 获取绑定相关信息(手机号、微信、QQ)
     */
    public function getBoundInfo(Request $request, UserService $userService)
    {
        $user = $this->identity()->getUser();
        $result = $userService->getBoundInfo($user['id']);
        return $this->response->setResponseData($result)->getResponse();
    }

    /**
     * 解除帐号的绑定(微信、QQ)
     */
    public function unboundOauth(Request $request, OauthorService $oauthorService)
    {
        $this->validate($request, [
            'openid' => 'required',
        ]);
        $openid = $request->input('openid', '');
        $where['open_id'] = $openid;
        $where['status'] = 0;
        if(!$oauthorService->getOauther($where)){
            throw new ServiceException(ServiceCode::SERVICE_PARAM_INVALID, '', []);
        }
        $data['status'] = 1;
        if ($oauthorService->updateLoginOauth($where, $data)) {
            $result['status'] = 1;
        }
        return $this->response->setResponseData($result)->getResponse();
    }

    /**
     * 获取腾讯云上传图片相关参数
     */
    public function getTencentAuth()
    {
        // 使用腾讯SDK生成上传图片路径URI
        $data['uri'] = ImageV2::generateResUrl(ImageService::BUCKET_PASSWORD, 0);
        // 制作腾讯云签名
        $data['auth'] = ImageService::makeTencentSign(ImageService::BUCKET_PASSWORD);
        return $this->response->setResponseData($data)->getResponse();
    }

}